package com.situ.javase.week3.Day3;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

import org.ietf.jgss.Oid;
import org.junit.Test;

public class IODemo {
	public static void main1(String[] args) {
		FileReader fileReader = null;
		try {
			 fileReader = new FileReader("io.txt");
			//读取单个字符，如果已经读到流的末尾，则返回-1；
			int ch1 = fileReader.read();
			System.out.println(ch1);
			System.out.println((char)ch1);
			int ch2 = fileReader.read();
			System.out.println(ch2);
			System.out.println((char)ch2);
			int ch3 = fileReader.read();
			System.out.println(ch3);
			System.out.println((char)ch3);
			int ch4 = fileReader.read();
			System.out.println(ch4);
			System.out.println((char)ch4);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//如果不加if   空指针错误
			if (fileReader != null) {
			try {
				fileReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
	
	
	public static void main2(String[] args) {
		FileReader fileReader = null;
		try {
			 fileReader = new FileReader("io.txt");
			 int ch = -1;
			 while((ch=fileReader.read()) != -1 ) {
				 System.out.println((char)ch); 
				 }
		}catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					//如果不加if   空指针错误
					if (fileReader != null) {
					try {
						fileReader.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}	 
}	
	
	
	public static void main3(String[] args) {
			FileReader fileReader = null;
			try {
				 fileReader = new FileReader("io.txt");
				 //public int read (char[] cbuf)
				 //将字符读入数组buffer
				 //1.没有达到流的末尾，返回的是读取的字符个数
				 //2.如果已达到流的末尾，则返回-1
				 char[] buffer = new char[10];
				 int length = 0;
				 while((length=fileReader.read(buffer)) != -1 ) {
					 System.out.println("length" + length); 
					 System.out.println(Arrays.toString(buffer));
					 
					 }
			}catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}finally {
						//如果不加if   空指针错误
						if (fileReader != null) {
						try {
							fileReader.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}	 
			}	
	
	public static void main5(String[] args) {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			 fileReader = new FileReader("io.txt");
			 fileWriter = new FileWriter("Oid.txt");
			 //public int read (char[] cbuf)
			 //将字符读入数组buffer
			 //1.没有达到流的末尾，返回的是读取的字符个数
			 //2.如果已达到流的末尾，则返回-1
			 char[] buffer = new char[10];
			 int length = 0;
			 while((length=fileReader.read(buffer)) != -1 ) {
				 System.out.println("length" + length); 
				 System.out.println(Arrays.toString(buffer));
				 
				 }
		}catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					//如果不加if   空指针错误
					if (fileReader != null) {
					try {
						fileReader.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}	 
		}	
	
	
	public static void main(String[] args) {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			 fileReader = new FileReader("io.txt");
			 fileWriter = new FileWriter("io_back.txt");
			 char [] buffer = new char[10];
			 int length = 0;
			 while((length = fileReader.read(buffer)) != -1) {
			 System.out.println("length" + length);
			 System.out.println(Arrays.toString(buffer));
			 fileWriter.write(buffer, 0, length);
			 }
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally  {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	@Test
	public  void fileInputOutputStreamCopy() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileInputStream = new FileInputStream("tou.jpg");
			fileOutputStream = new FileOutputStream("touxiang.jpg");
			byte [] buffer = new byte[1024];
			int length = 0;
			while ((length = fileInputStream.read(buffer))!= -1) {
				System.out.println("length" + length);
				 System.out.println(Arrays.toString(buffer));
				fileOutputStream.write(buffer, 0, length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally  {
		if (fileInputStream != null) {
			try {
				fileInputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}if (fileOutputStream != null) {
			
		}try {
			fileOutputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}
	}
}